package com.bocai.eelly.activitys;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.eelly.R;
import com.bocai.eelly.adapter.DialogAdapter;
import com.bocai.eelly.bean.DialogBean;
import com.bocai.eelly.common.BaseActivity;
import com.bocai.eelly.common.Loading;
import com.bocai.eelly.common.ToolbarHelper;
import com.bocai.eelly.event.MainEvent;
import com.bocai.eelly.net.ApiManage;
import com.bocai.eelly.net.Url;
import com.bocai.eelly.utils.BitmapUtils;
import com.bocai.eelly.utils.DateTimePickDialogUtil;
import com.bocai.eelly.utils.DialogUtil;
import com.bocai.eelly.utils.L;
import com.bocai.eelly.utils.OkHttpClientManager;
import com.bocai.eelly.utils.T;
import com.bocai.eelly.utils.TimeUtils;
import com.bocai.eelly.utils.Utils;
import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.OnItemClickListener;
import com.squareup.okhttp.Request;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.functions.Action1;


/**
 * 发布面料询价
 */
public class PostFabricActivity extends BaseActivity implements View.OnClickListener{

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.tv_post_fabric_type)
    TextView tvPostFabricType;
    @Bind(R.id.ll_post_fabric_type)
    LinearLayout llPostFabricType;
    @Bind(R.id.et_post_fabric_name)
    EditText etPostFabricName;
    @Bind(R.id.tv_post_fabric_component_name)
    TextView tvPostFabricComponentName;
    @Bind(R.id.ll_post_fabric_component_name)
    LinearLayout llPostFabricComponentName;
    @Bind(R.id.et_post_fabric_yarn_specification)
    EditText etPostFabricYarnSpecification;
    @Bind(R.id.et_post_fabric_weight)
    EditText etPostFabricWeight;
    @Bind(R.id.tv_post_fabric_weight)
    TextView tvPostFabricWeight;
    @Bind(R.id.et_post_fabric_wight)
    EditText etPostFabricWight;
    @Bind(R.id.tv_post_fabric_wight)
    TextView tvPostFabricWight;
    @Bind(R.id.tv_post_fabric_technology)
    TextView tvPostFabricTechnology;
    @Bind(R.id.ll_post_fabric_technology)
    LinearLayout llPostFabricTechnology;
    @Bind(R.id.et_post_fabric_num)
    EditText etPostFabricNum;
    @Bind(R.id.tv_post_fabric_num)
    TextView tvPostFabricNum;
    @Bind(R.id.tv_post_fabric_time)
    TextView tvPostFabricTime;
    @Bind(R.id.ll_post_fabric_time)
    LinearLayout llPostFabricTime;
    @Bind(R.id.et_post_fabric_remark)
    EditText etPostFabricRemark;
    @Bind(R.id.ll_post_fabric_choose_photo)
    LinearLayout llPostFabricChoosePhoto;
    @Bind(R.id.tv_post_fabric_commit)
    TextView tvPostFabricCommit;
    private ArrayList<String> photos=new ArrayList<>();
    private File[] files;
    private String [] fileKeys;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_fabcir);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    @Override
    public void onEventMainThread(MainEvent event) {
        super.onEventMainThread(event);
        if (Utils.EVENTMSG.equals(event.msg)) {
            photos = event.photos;
            L.d(photos.get(0).toString());
            fileKeys = new String[photos.size()];
            files = new File[photos.size()];
            for (int i = 0; i < photos.size(); i++) {
                files[i] = new File(BitmapUtils.scal(photos.get(i)));
                fileKeys[i] = "photo" + (i + 1);
            }
        }

    }
    private void initView() {
        ToolbarHelper.setup(this, R.string.str_title_post_xunjia, R.mipmap.ic_back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

    }

    private void initEvent() {
        llPostFabricTime.setOnClickListener(this);
        llPostFabricComponentName.setOnClickListener(this);
        llPostFabricTechnology.setOnClickListener(this);
        llPostFabricType.setOnClickListener(this);
        tvPostFabricWeight.setOnClickListener(this);
        tvPostFabricWight.setOnClickListener(this);
        tvPostFabricNum.setOnClickListener(this);
        tvPostFabricCommit.setOnClickListener(this);
        llPostFabricChoosePhoto.setOnClickListener(this);
    }

    private String componentName="棉";
    @Override
    public void onClick(final View v) {
        switch (v.getId())
        {
            case R.id.ll_post_fabric_component_name:

                View dialogView= LayoutInflater.from(this).inflate(R.layout.dialog_layout_chengfen,null);
                final AlertDialog dialog = new AlertDialog.Builder(this)
                        .setView(dialogView).show();
                final TextView tvChangsi= (TextView) dialog.findViewById(R.id.tv_changsi);
                final TextView tvMian= (TextView) dialog.findViewById(R.id.tv_mian);
                final EditText etChengfen=(EditText)dialog.findViewById(R.id.et_chengfen);
                tvChangsi.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        componentName=tvChangsi.getText().toString();
                        tvChangsi.setTextColor(getResources().getColor(R.color.cl_app_color));
                        tvMian.setTextColor(getResources().getColor(R.color.cl_text_373737));
                    }
                });
                tvMian.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        componentName=tvMian.getText().toString();
                        tvMian.setTextColor(getResources().getColor(R.color.cl_app_color));
                        tvChangsi.setTextColor(getResources().getColor(R.color.cl_text_373737));
                    }
                });
                dialog.findViewById(R.id.tv_add).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(Utils.isEmpty(etChengfen.getText().toString()))
                        {
                            T.showShort(PostFabricActivity.this,"请输入面料成分");
                        }else{
                            tvPostFabricComponentName.setText(etChengfen.getText().toString()+"%"+componentName);
                            dialog.dismiss();

                        }

                    }
                });
                break;

            case R.id.ll_post_fabric_type:
                String timeline= Utils.getTimeline();
                String sign=Utils.md5(timeline);
                ApiManage.get_category(this, timeline, sign, Utils.getUserId(this, "id"), new Action1<DialogBean>() {
                    @Override
                    public void call(DialogBean bean) {
                        if(Utils.checkResultCode(bean.getReturn_code()))
                        {
                            List<String > types=new ArrayList<>();
                            for (int i=0;i<bean.getData().size();i++)
                            {
                                types.add(bean.getData().get(i).getTitle());
                            }
                            DialogUtil.showDialog(PostFabricActivity.this, new DialogAdapter(PostFabricActivity.this,types), new OnItemClickListener() {
                                @Override
                                public void onItemClick(DialogPlus dialog, Object item, View view, int position) {

                                    tvPostFabricType.setText(item.toString());
                                    dialog.dismiss();
                                }
                            });
                        }
                    }
                });
                break;
            case R.id.ll_post_fabric_time:
                new DateTimePickDialogUtil(this).dateTimePicKDialog(tvPostFabricTime);
                break;
            case R.id.ll_post_fabric_technology:
                final List<String> technologys=new ArrayList<>();
                String [] technology={"坏布","半漂","染色","印花","色织","喷绘","烂花","手绘","碧纹","涂层","扎染","吊染","蜡染","剪花","其他"};
                for (int i=0;i<technology.length;i++)
                {
                    technologys.add(technology[i]);
                }
                DialogUtil.showGridViewDialog(this, new DialogAdapter(this, technologys), new OnItemClickListener() {
                    @Override
                    public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                        tvPostFabricTechnology.setText(technologys.get(position));
                        dialog.dismiss();
                    }
                });
                break;

            case R.id.tv_post_fabric_weight:
                List<String> weights = new ArrayList<>();

                String[] weight = {"克/平方米", "克/平方码","姆米"};
                for (int i = 0; i < weight.length; i++) {
                    weights.add(weight[i]);
                }
                DialogUtil.showDialog(this, new DialogAdapter(this, weights), new OnItemClickListener() {
                    @Override
                    public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                        tvPostFabricWight.setText(item.toString());
                        dialog.dismiss();
                    }
                });
                break;
            case R.id.tv_post_fabric_wight:
                List<String> wights = new ArrayList<>();
                String[] wight = {"厘米", "英吋"};
                for (int i = 0; i < wight.length; i++) {
                    wights.add(wight[i]);
                }
                DialogUtil.showDialog(this, new DialogAdapter(this, wights), new OnItemClickListener() {
                    @Override
                    public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                        tvPostFabricWight.setText(item.toString());
                        dialog.dismiss();
                    }
                });
                break;
            case R.id.tv_post_fabric_num:
                List<String> nums = new ArrayList<>();
                String[] num = {"个", "件"};
                for (int i = 0; i < num.length; i++) {
                    nums.add(num[i]);
                }
                DialogUtil.showDialog(this, new DialogAdapter(this, nums), new OnItemClickListener() {
                    @Override
                    public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                        tvPostFabricNum.setText(item.toString());
                        dialog.dismiss();
                    }
                });
                break;
            case R.id.ll_post_fabric_choose_photo:
                Intent intent=new Intent(this,ChoosePhotosActivity.class);
                intent.putExtra("photos",photos);
                startActivity(intent);
                break;
            case R.id.tv_post_fabric_commit:
                if (files == null || files.length == 0) {
                    T.showShort(this, "请选择照片");
                } else {
                    String delivery_date = tvPostFabricTime.getText().toString();
                    String category=tvPostFabricType.getText().toString();
                    String name = etPostFabricName.getText().toString();
                    String component_name = tvPostFabricComponentName.getText().toString();
                    String yarn_specification = etPostFabricYarnSpecification.getText().toString();
                    String weightname = etPostFabricWeight.getText().toString();
                    String width = etPostFabricWight.getText().toString();
                    String technologyname = tvPostFabricTechnology.getText().toString();
                    String number = etPostFabricNum.getText().toString();
                    if (Utils.isEmpty(category)) {
                        T.showShort(this, "请选择面料类别");
                    } else if (Utils.isEmpty(name)) {
                        T.showShort(this, "请输入面料名称");
                    } else if (Utils.isEmpty(number)) {
                        T.showShort(this, "请输入产品数量");
                    } else if (Utils.isEmpty(component_name)) {
                        T.showShort(this, "请输入面料成分比例");
                    } else if (Utils.isEmpty(yarn_specification)) {
                        T.showShort(this, "请输入纱支规格");
                    } else if (Utils.isEmpty(weightname)) {
                        T.showShort(this, "请输入克重数值");
                    } else if (Utils.isEmpty(width)) {
                        T.showShort(this, "请输入门幅数量");
                    } else if (Utils.isEmpty(technologyname)) {
                        T.showShort(this, "请选择产品工艺");
                    } else if (Utils.isEmpty(delivery_date)) {
                        T.showShort(this, "请选择交货日期");
                    } else {
                        Loading.show(this, getString(R.string.str_loading));
                        commit();
                    }
                }
                break;
        }
    }
    public OkHttpClientManager.Param[] getParams(String category,String name, String component_name, String yarn_specification, String weight, String weight_unit,
                                                 String width, String width_unit, String technology, String number, String  number_unit,
                                                 String delivery_date,String remarks){
        String timeline=Utils.getTimeline();
        String sign=Utils.md5(timeline);
        return new OkHttpClientManager.Param[]{
                new OkHttpClientManager.Param("timeline", timeline),
                new OkHttpClientManager.Param("sign", sign),
                new OkHttpClientManager.Param("userid", Utils.getUserId(this,"id")),
                new OkHttpClientManager.Param("category", category),
                new OkHttpClientManager.Param("name", name),
                new OkHttpClientManager.Param("component_name", component_name),
                new OkHttpClientManager.Param("yarn_specification", yarn_specification),
                new OkHttpClientManager.Param("weight", weight),
                new OkHttpClientManager.Param("weight_unit", weight_unit),
                new OkHttpClientManager.Param("width", width),
                new OkHttpClientManager.Param("width_unit", width_unit),
                new OkHttpClientManager.Param("technology", technology),
                new OkHttpClientManager.Param("number", number),
                new OkHttpClientManager.Param("number_unit", number_unit),
                new OkHttpClientManager.Param("delivery_date", delivery_date),
                new OkHttpClientManager.Param("remarks", remarks)};
    }
    private void commit() {
        String delivery_date = TimeUtils.dataOne(tvPostFabricTime.getText().toString());
        try {
            OkHttpClientManager.postAsyn(Url.BASEURL + Url.POST_FABRIC_INQUIRY, new OkHttpClientManager.ResultCallback<String>() {
                @Override
                public void onError(Request request, Exception e) {
                    Loading.dismiss();
                    T.showShort(PostFabricActivity.this,e.toString());
                }

                @Override
                public void onResponse(String response) {
                    Loading.dismiss();
                    L.d(response);
                    T.showShort(PostFabricActivity.this, response);
                }
            },files,fileKeys,getParams(tvPostFabricType.getText().toString(),etPostFabricName.getText().toString(),tvPostFabricComponentName.getText().toString(),
                    etPostFabricYarnSpecification.getText().toString(),etPostFabricWeight.getText().toString(),tvPostFabricWeight.getText().toString(),
                    etPostFabricWight.getText().toString(),tvPostFabricWight.getText().toString(),tvPostFabricTechnology.getText().toString(),
                    etPostFabricNum.getText().toString(),tvPostFabricNum.getText().toString(),delivery_date,etPostFabricRemark.getText().toString()));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}
